package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wx.mall.operate.dto.RebGoodsDto;
import com.wx.mall.operate.dto.RebGoodsTwoDto;
import com.wx.mall.operate.entity.RebGoods;
import com.wx.mall.operate.mapper.RebGoodsMapper;
import com.wx.mall.operate.service.IRebGoodsService;
import com.wx.mall.operate.service.IVGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author z
 * @since 2020-6-4
 */
@Service
public class RebGoodsServiceImpl extends ServiceImpl<RebGoodsMapper, RebGoods> implements IRebGoodsService {

    @Autowired
    IVGoodsService ivGoodsService;

    @Autowired
    IRebGoodsService iRebGoodsService;

    @Override
    public List<RebGoods> findAllGoods(RebGoodsDto dto) {
        iRebGoodsService.findGoods();
        ivGoodsService.findGoodsA(dto.getIds());
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<RebGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        List<RebGoods> list2 = this.list(queryWrapper);
        return list2;
    }

    @Override
    public int findGoods() {
        QueryWrapper<RebGoods> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        List<RebGoods> list = this.list(queryWrapper);
        int bba=0;
        for (RebGoods v:list
             ) {
            RebGoods rebGoods=new RebGoods();
            rebGoods.setId(v.getId());
            RebGoods byId = iRebGoodsService.getById(rebGoods.getId());
            byId.setStatus(0);
            boolean b = iRebGoodsService.updateById(byId);
            if (b==true){
                bba=1;
            }else {
                bba=0;
            }
        }
        return bba;
    }

    @Override
    public List<RebGoods> findScreenGoods(RebGoodsTwoDto dto) {
        iRebGoodsService.findGoods();
        ivGoodsService.findGoodsA(dto.getIds());
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<RebGoods> queryWrapper = new QueryWrapper<>();
        if (dto.getBrandId()!=null){
            queryWrapper.eq("brand_id",dto.getBrandId());
        }else if (dto.getSortId()!=null){
            queryWrapper.eq("sort_id",dto.getSortId());
        }else if (dto.getTitle()!=null){
            queryWrapper.like("title",dto.getTitle());
        }
        List<RebGoods> list2 = this.list(queryWrapper);
        return list2;
    }
}
